package com.yingda.electronicscaleadvices.activity.screen;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.FileProvider;

import com.yingda.electronicscaleadvices.R;
import com.yingda.electronicscaleadvices.manege.BaseActivity;
import com.yingda.electronicscaleadvices.utils.StatusBarUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;


public class OutboundActivity extends BaseActivity implements View.OnClickListener {

    static final int REQUEST_IMAGE_CAPTURE = 1;
    Uri photoUri;
    @BindView(R.id.rl_base)
    LinearLayout rl_base;

    @BindView(R.id.iv_out_photo)
    ImageView iv_out_photo;

    @BindView(R.id.tv_out_phone)
    TextView tv_out_phone;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.out_bound_layout);

        ButterKnife.bind(this);
        //设置背景标题
        StatusBarUtil.showTransparentStatusBarPadding(this, rl_base);
        initListener();

    }

    private void initListener() {
        tv_out_phone.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_out_phone:
                openCamera();
                break;
        }
    }
    //打开相机拍照
    private void openCamera() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            File photoFile = null;
            try {
                photoFile = createImageFile();
                if (photoFile != null) {
                    photoUri = FileProvider.getUriForFile(this, "com.yingda.electronicscaleadvices.fileprovider", photoFile);
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
                    startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
                }


            } catch (IOException e) {
                e.printStackTrace();
            }

        }


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bitmap bitmap = null;
            try {
                 bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(photoUri));
                iv_out_photo.setImageBitmap(bitmap);  // 显示在ImageView中，或根据需要处理
                tv_out_phone.setText("重新拍照");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

        }
    }


    // todo 需要删除图片
    private File createImageFile() throws IOException {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        return File.createTempFile(imageFileName, ".jpg", storageDir);
    }

}
